from unicodedata import category

from odoo import models, fields, api, _
from odoo.exceptions import UserError, ValidationError
from odoo.http import request


class ProductCategory(models.Model):
    _inherit = 'product.category'
    _order = 'code ASC'

    code = fields.Char(string="Category Code")
    product_category_coding_list_ids = fields.One2many(
        'product.category.coding.list', 'product_category_id', string="Coding Attributes",
        ondelete='restrict', copy=True)

    # code_rule_type = fields.Selection(
    #     string='Code Rule Type',
    #     selection=[
    #         ('complex', 'Complex Attribute'),
    #         ('flow', 'Simple Flow'), ]
    #     , default='complex', readonly=True)

    flow_number = fields.Integer(string="Flow Number", default=1, copy=False)
    # is_approve = fields.Boolean(string="Approve", default=True)

    en_name = fields.Char(string='English name')
    description_chi = fields.Char('description(chinese)')
    description_eng = fields.Char('description(english)')
    notes = fields.Char(string='notes')
    coding_specification = fields.Html(string="Coding specification")
    f_code = fields.Char(related="parent_id.code")
    # 提示
    coding_note = fields.Text(string="Coding Note", translate=True)
    @api.onchange('parent_id')
    def onchange_parent_id(self):
        for rec in self:
            if rec.parent_id:
                rec.coding_specification = rec.parent_id.coding_specification
                rec.coding_note = rec.parent_id.coding_note

            else:
                rec.coding_specification = False
                rec.coding_note = False


    @api.depends('name', 'parent_id.complete_name', 'en_name')
    def _compute_complete_name(self):
        for category in self:
            if category.en_name == category.name:
                name = category.name
            elif not category.en_name:
                name = category.name
            else:
                name = '%s-%s' % (category.name, category.en_name)
            # category.complete_name = name
            if category.parent_id:
                category.complete_name = '%s / %s' % (category.parent_id.complete_name, name)
            else:
                category.complete_name = name

    @api.depends('name', 'parent_id.complete_name', 'en_name')
    def _compute_display_name(self):
        for category in self:
            if category.en_name == category.name:
                name = category.name
            elif not category.en_name:
                name = category.name
            else:
                name = '%s-%s' % (category.name, category.en_name)
            # category.display_name = name
            if category.parent_id:
                category.display_name = '%s / %s' % (category.parent_id.display_name, name)
            else:
                category.display_name = name

    def write(self, vals):
        res = super(ProductCategory, self).write(vals)
        for rec in self:
            childs = self.search([('parent_id','=',rec.id)])
            if 'coding_specification' in vals and childs:
                childs.coding_specification=rec.coding_specification
            if 'coding_note' in vals and childs:
                childs.coding_note=rec.coding_note
        return res

    @api.constrains('product_category_coding_list_ids')
    def set_attribute(self):
        for record in self:
            if record.parent_id:
                continue
            self.set_attribute_child(record.child_id, self.product_category_coding_list_ids)



    def set_attribute_child(self, category_ids, product_category_coding_list_ids):
        for record in category_ids:
            if record.child_id:
                record.product_category_coding_list_ids.unlink()
                record.write({
                    'product_category_coding_list_ids': [(0, 0, {
                        'sb_material_coding_attribute_id': i.sb_material_coding_attribute_id.id,
                        'sequence': i.sequence,
                        'coding_description': i.coding_description,
                        'coding_participation': i.coding_participation,
                    }) for i in product_category_coding_list_ids]
                })
                record.set_attribute_child(record.child_id, product_category_coding_list_ids)
            else:
                record.product_category_coding_list_ids.unlink()
                record.write({
                    'product_category_coding_list_ids': [(0, 0, {
                        'sb_material_coding_attribute_id': i.sb_material_coding_attribute_id.id,
                        'sequence': i.sequence,
                        'coding_description': i.coding_description,
                        'coding_participation': i.coding_participation,
                    }) for i in product_category_coding_list_ids]
                })


class ProductCategoryCodingList(models.Model):
    _name = 'product.category.coding.list'
    _description = 'Category Coding Attribute List'
    _order = 'sequence DESC'

    sb_material_coding_attribute_id = fields.Many2one('sb.material.coding.attribute', string="Coding Attribute",
                                                      ondelete="restrict")
    sequence = fields.Integer(string='Coding Sequence')
    # 参与描述
    coding_description = fields.Boolean(string="Coding Description", default=False)
    # 参与编码
    coding_participation = fields.Boolean(string="Coding Participation", default=False)
    coding_attribute_type = fields.Selection(related="sb_material_coding_attribute_id.coding_attribute_type",
                                             string="Attribute Type")
    product_category_id = fields.Many2one('product.category', ondelete="cascade")
